package com.hmyd.hmyd_android.shop;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.lang.reflect.Type;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.common.base.Objects;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.adapter.ShopCarAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Address;
import com.hmyd.hmyd_android.dto.DtoInfoNoPage;
import com.hmyd.hmyd_android.dto.GoodDetails;
import com.hmyd.hmyd_android.dto.InfoDto;
import com.hmyd.hmyd_android.ui.BasicActivity;
import com.hmyd.hmyd_android.ui.LoginActivity;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.PreferencesUtils;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.MyDialog;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

/**
 * 购物车
 * 
 * @author wangk
 *
 */
@ContentView(R.layout.activity_jf_mall_shop_cart)
public class JfMallShopCartActivity extends BasicActivity {

	@ViewInject(R.id.chooseAdd)
	private RelativeLayout mAddressLayout; // 地址填充
	@ViewInject(R.id.sureBlank)
	private RelativeLayout bottomLayout; // 底部提交
	@ViewInject(R.id.shopContent)
	private View content; // 除标题栏所有布局

	@ViewInject(R.id.chooseAddDefault)
	private RelativeLayout mTvAddress; // 无默认地址的默认显示
	@ViewInject(R.id.chooseTextView)
	private TextView chooseTextView; // 无默认地址或未登录显示得文本
	private Context mContext;

	@ViewInject(R.id.avaliableScore)
	private TextView mTvTotalScore; // 剩余积分
	@ViewInject(R.id.copeScore)
	private TextView mTvNeedPay; // 需要支付积分
	@ViewInject(R.id.recName)
	private TextView mTvAddressName; // 接收者姓名
	@ViewInject(R.id.recPhone)
	private TextView mTvPhone; // 接受者手机号码
	@ViewInject(R.id.recAddress)
	private TextView mTvAddressDetail; // 接受者地址详细信息
	@ViewInject(R.id.shopMessage)
	private EditText mEdRemark; // 订单留言
	@ViewInject(R.id.emptymall)
	private ImageView mIvEmpty; // 购物车为空的时候的默认显示图片
	@ViewInject(R.id.changeAddress)
	private RelativeLayout changeAddress;// 切换地址
	@ViewInject(R.id.home_title_txt)
	private TextView home_title_txt;
	@ViewInject(R.id.layout_home_title)
	private View layout_home_title;
	@ViewInject(R.id.home_right)
	private TextView home_right;

	// dialog
	private MyDialog myDialog;
	private ImageView virtual_close; // 弹出框关闭按钮
	private RadioGroup pay_way; // 弹出框要充值的号码
	private TextView native_sel_te; // 弹出框本机号码
	private EditText remark1; // 弹出框充值号码
	private TextView virtual_confirm; // 确定提交
	private RadioButton native_sel;// 本机号码
	private RadioButton other_sel;// 其他号码
	private String remarkStr; // 重置弹出框字段

	private List<String> phone_pay_nums = new ArrayList<String>();

	private List<GoodDetails> datas = new ArrayList<GoodDetails>();
	private Address address;

	private static int CHOOSE_ADDRESS = 1;
	private static int SUBMMIT_ORDRE = 2;

	@ViewInject(R.id.shopCartList)
	private ListView mListView;

	private ShopCarAdapter adapter;
	private SharedPreferences sharedPreferences;
	private Editor editor;
	private boolean isFromAddress = false;

	private String remarkString;

	private boolean havePhonePay = false;

	private List<GoodDetails> checkGoods; // 已经被check的商品

	private OnGlobalLayoutListener globalLayoutListener;

	private int sureBlankOrgHeight;
	private int oldUnDisplayHeight;

	private ValueAnimator sureBlankHideAnimal; // 底部确认栏隐藏动画
	private ValueAnimator sureBlankShowAnimal; // 底部确认栏显示动画
	private RotateAnimation arrowRotate; // 箭头旋转

	@ViewInject(R.id.sureBlankCon)
	private ImageView sureBlankCon;

	private boolean sureBlankisUp = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	private void getDatas() {
		// 购物车信息
		String name = sharedPreferences.getString("data", null);
		try {
			if (!TextUtils.isEmpty(name)) {
				String2List(name);
			} else {
				mIvEmpty.setVisibility(View.VISIBLE);
				content.setVisibility(View.GONE);
				bottomLayout.setVisibility(View.GONE);
				home_right.setVisibility(View.GONE);
			}
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 获取默认地址信息
		String defaultAddress = sharedPreferences.getString("address", "");
		if (!defaultAddress.equals("")) {
			byte[] addressBytes = Base64.decode(defaultAddress.getBytes(),
					Base64.DEFAULT);
			ByteArrayInputStream bais = new ByteArrayInputStream(addressBytes);
			try {
				ObjectInputStream bis = new ObjectInputStream(bais);
				try {
					address = (Address) bis.readObject();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} finally {
					bis.close();
				}
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		if (AppBasic.isLogin && address != null) {
			mAddressLayout.setVisibility(View.VISIBLE);
			mTvAddress.setVisibility(View.GONE);
			mTvAddressName.setText(address.addressee);
			mTvAddressDetail.setText(address.province + address.city
					+ address.area + address.address);
			mTvPhone.setText(address.cellPhoneNumber);
			// mTvFixPhone.setText(address.fixedTelephone);
		} else if (AppBasic.isLogin && address == null) {
			mAddressLayout.setVisibility(View.GONE);
			mTvAddress.setVisibility(View.VISIBLE);
			chooseTextView.setText("请选择地址");
		} else {
			chooseTextView.setText("请登录");
		}
	}

	public List<GoodDetails> String2List(String ListString)
			throws StreamCorruptedException, IOException,
			ClassNotFoundException {
		byte[] mobileBytes = Base64.decode(ListString.getBytes(),
				Base64.DEFAULT);
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				mobileBytes);
		ObjectInputStream objectInputStream = new ObjectInputStream(
				byteArrayInputStream);
		@SuppressWarnings("unchecked")
		List<GoodDetails> carList = (List<GoodDetails>) objectInputStream
				.readObject();
		objectInputStream.close();
		datas.addAll(carList);
		if (datas.size() == 0) {
			mIvEmpty.setVisibility(View.VISIBLE);
			content.setVisibility(View.GONE);
			bottomLayout.setVisibility(View.GONE);
			home_right.setVisibility(View.GONE);
		} else {
			mIvEmpty.setVisibility(View.GONE);
			content.setVisibility(View.VISIBLE);
			bottomLayout.setVisibility(View.VISIBLE);
			home_right.setVisibility(View.VISIBLE);
		}
		return datas;
	}

	private void initViews() {
		mContext = this;
		sureBlankOrgHeight = bottomLayout.getLayoutParams().height; // 获取底部确定框高度
		home_title_txt.setText("购物车");
		home_right.setText("全选");
		layout_home_title.setOnTouchListener(touch);
		if (AppBasic.isLogin) {
			mTvTotalScore.setText(NumberFormat.getInstance().format(
					AppBasic.totalScore));
		}

		sharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE);
		editor = sharedPreferences.edit();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isFromAddress) {
			return;
		}
		// 获取默认地址信息
		String defaultAddress = sharedPreferences.getString("address", "");
		if (!defaultAddress.equals("")) {
			byte[] addressBytes = Base64.decode(defaultAddress.getBytes(),
					Base64.DEFAULT);
			ByteArrayInputStream bais = new ByteArrayInputStream(addressBytes);
			try {
				ObjectInputStream bis = new ObjectInputStream(bais);
				try {
					address = (Address) bis.readObject();
					bis.close();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (AppBasic.isLogin && address != null) {
			mAddressLayout.setVisibility(View.VISIBLE);
			mTvAddress.setVisibility(View.GONE);

			mTvAddressName.setText(address.addressee);
			mTvAddressDetail.setText(address.province + address.city
					+ address.area + address.address);
			mTvPhone.setText(address.cellPhoneNumber);
			// mTvFixPhone.setText(address.fixedTelephone);
			home_right.setVisibility(View.VISIBLE);
		} else if (AppBasic.isLogin && address == null) {
			mAddressLayout.setVisibility(View.GONE);
			home_right.setVisibility(View.GONE);
			mTvAddress.setVisibility(View.VISIBLE);
			chooseTextView.setText("请选择地址");
		} else {
			chooseTextView.setText("请登录");
		}

		if (AppBasic.isLogin) {
			mTvTotalScore.setText(NumberFormat.getInstance().format(
					AppBasic.totalScore));
		}
		datas.clear(); // 清空显示商品
		checkGoods = adapter.getCheckedGoods();
		if (checkGoods != null) {
			checkGoods.clear(); // 清空已选择商品
			adapter.getCheckedGoods().clear();
			mTvNeedPay.setText(0 + "");
		}
		String name = sharedPreferences.getString("data", null);
		try {
			if (!TextUtils.isEmpty(name)) {
				String2List(name);
			} else {
				mIvEmpty.setVisibility(View.VISIBLE);
				content.setVisibility(View.GONE);
				bottomLayout.setVisibility(View.GONE);
			}
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		adapter.notifyDataSetChanged();
		// mEdRemark.setText("");
	}

	@Override
	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		Type type1 = new TypeToken<DtoInfoNoPage<InfoDto>>() {
		}.getType();
		DtoInfoNoPage<InfoDto> dtoInfoNoPage1 = gson.fromJson(result, type1);
		if(dtoInfoNoPage1 != null && dtoInfoNoPage1.suc == 1011) {
			for (GoodDetails goodDetails : checkGoods) {
				if (datas.contains(goodDetails)) {
					datas.remove(goodDetails);
				}
			}
			editor.putString("data", "");
			editor.commit();
			mEdRemark.setText("");
			adapter.setCheckedGoodsToNull(); // 清空用于存储选择商品的集合
			if (!datas.isEmpty()) {
				try {
					List2String(datas);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			MyToast.showHMToast(JfMallShopCartActivity.this, true, "订单提交成功");
			startActivity(new Intent(mContext, MyOrderActivity.class));
		} else {
			MyToast.showHMToast(JfMallShopCartActivity.this, false,
					dtoInfoNoPage1.errmsg);
		}
	}

	private void List2String(List<GoodDetails> datas) throws IOException {

		// 实例化一个ByteArrayOutputStream对象，用来装载压缩后的字节文件。
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		// 然后将得到的字符数据装载到ObjectOutputStream
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				byteArrayOutputStream);
		// writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它
		objectOutputStream.writeObject(datas);
		// 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
		String ListString = new String(Base64.encode(
				byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
		// 关闭objectOutputStream
		objectOutputStream.close();

		// 用putString的方法保存数据
		editor.putString("data", ListString);
		// 提交当前数据
		editor.commit();

	}

	@OnClick({ R.id.layout_address, R.id.changeAddress, R.id.orderSubimt,
			R.id.chooseAddDefault, R.id.sureBlankCon })
	public void onClick(View v) {
		Intent intent = new Intent();
		intent.putExtra("starter", 1);
		intent.setClass(mContext, JfMallAdresActivity.class);
		switch (v.getId()) {
		case R.id.layout_address:
			startActivityForResult(intent, CHOOSE_ADDRESS);
			break;
		case R.id.changeAddress:
			if (AppBasic.isLogin) {
				startActivityForResult(intent, CHOOSE_ADDRESS);
			} else {
				startActivity(new Intent(mContext, LoginActivity.class));
			}
			break;
		case R.id.orderSubimt:
			if (AppBasic.isLogin) {
				if (!isNull()) {
					submitOrders();
				}

			} else {
				startActivity(new Intent(mContext, LoginActivity.class));
			}
			break;
		case R.id.chooseAddDefault:
			if (AppBasic.isLogin) {
				startActivityForResult(intent, CHOOSE_ADDRESS);
			} else {
				startActivity(new Intent(mContext, LoginActivity.class));
			}
			break;
		case R.id.sureBlankCon:
			if (sureBlankisUp) {
				sureBlankHideAnimal.start();
				arrowRotate.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						Bitmap bitmap = BitmapFactory.decodeResource(
								getResources(), R.drawable.list_arrow_up);
						sureBlankCon.setImageBitmap(bitmap);
					}
				});
				sureBlankCon.setAnimation(arrowRotate);
				arrowRotate.startNow();
				sureBlankisUp = false;
			} else {
				sureBlankShowAnimal.start();
				arrowRotate.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						Bitmap bitmap = BitmapFactory.decodeResource(
								getResources(), R.drawable.list_arrow_down);
						sureBlankCon.setImageBitmap(bitmap);
					}
				});
				sureBlankCon.setAnimation(arrowRotate);
				arrowRotate.startNow();
				sureBlankisUp = true;
			}
			break;
		default:
			break;
		}

	}

	@Override
	protected void setParams() {
		initViews();
		getDatas();
		phone_pay_nums.add("hmyd05101");
		phone_pay_nums.add("hmyd05102");
		phone_pay_nums.add("hmyd05103");
		phone_pay_nums.add("hmyd05104");
		phone_pay_nums.add("hmyd05105");
		phone_pay_nums.add("hmyd05106");
		adapter = new ShopCarAdapter(mContext, datas, mTvNeedPay, mIvEmpty,
				content, bottomLayout,home_right);
		mListView.setAdapter(adapter);
		arrowRotate = new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		arrowRotate.setDuration(300);
		arrowRotate.setFillAfter(false); // 执行完动画，不停留在执行完后的位置

		sureBlankCon.setVisibility(View.GONE); // 默认隐藏确认框控制按钮
		final int distance = sureBlankOrgHeight / 14;
		sureBlankHideAnimal = ValueAnimator.ofFloat(0f, 1f);
		sureBlankHideAnimal.setDuration(300);
		sureBlankHideAnimal.setInterpolator(new LinearInterpolator());
		sureBlankHideAnimal.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator arg0) {
				LayoutParams params = bottomLayout.getLayoutParams();
				if (params.height > 0) {
					params.height = params.height - distance;
					bottomLayout.setLayoutParams(params);
				}
			}
		});
		sureBlankHideAnimal.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator arg0) {
			}

			@Override
			public void onAnimationRepeat(Animator arg0) {
			}

			@Override
			public void onAnimationEnd(Animator arg0) {
				LayoutParams params = bottomLayout.getLayoutParams();
				params.height = 0;
				bottomLayout.setLayoutParams(params);
			}

			@Override
			public void onAnimationCancel(Animator arg0) {
			}
		});

		sureBlankShowAnimal = ValueAnimator.ofFloat(0f, 1f);
		sureBlankShowAnimal.setDuration(300);
		sureBlankShowAnimal.setInterpolator(new LinearInterpolator());
		sureBlankShowAnimal.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator arg0) {
				LayoutParams params = bottomLayout.getLayoutParams();
				if (params.height < sureBlankOrgHeight) {
					params.height = params.height + distance;
					bottomLayout.setLayoutParams(params);
				}
			}
		});

		sureBlankShowAnimal.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator arg0) {
			}

			@Override
			public void onAnimationRepeat(Animator arg0) {
			}

			@Override
			public void onAnimationEnd(Animator arg0) {
				LayoutParams params = bottomLayout.getLayoutParams();
				params.height = sureBlankOrgHeight;
				bottomLayout.setLayoutParams(params);
			}

			@Override
			public void onAnimationCancel(Animator arg0) {
			}
		});

		controlKeybordLayout(
				getWindow().getDecorView().findViewById(android.R.id.content),
				mListView);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	/**
	 * 购物车全选功能
	 */
	@Override
	protected void rightButton() {
		checkGoods = adapter.checkAllItem();
	}

	/**
	 * 提交数据信息发送请求
	 */
	private void submitOrders() {
		remarkString = mEdRemark.getText().toString().trim();
		if (Utils.isEmojiCharacter(remarkString)) {
			MyToast.showHMToast(JfMallShopCartActivity.this, false, "禁止输入表情");
			return;
		}
		checkGoods = adapter.getCheckedGoods();
		if (Objects.equal(null, checkGoods) || checkGoods.isEmpty()) {
			MyToast.showHMToast(JfMallShopCartActivity.this, false, "请先选择商品");
			return;
		}
		final RequestParams params = new RequestParams();
		params.addBodyParameter("addressId", address.id);
		StringBuilder tempBuilder = new StringBuilder();
		final StringBuilder builder = new StringBuilder();
		for (GoodDetails goodDetails : checkGoods) {
			// 判断是否含有手机充值商品
			if (phone_pay_nums.contains(goodDetails.id)) {
				havePhonePay = true;
			}
			tempBuilder.append(goodDetails.id + "_" + goodDetails.number + ",");
		}
		builder.append(tempBuilder.substring(0, tempBuilder.lastIndexOf(","))); // 去掉最后一位的逗号
		if (havePhonePay) {
			// 初始化弹出框
			if(myDialog != null) {
				myDialog.show();
			} else {
				initVirtualconfirmDialog(params,builder);
				myDialog.show();
			}
			havePhonePay = false;
		} else {
			if (TextUtils.isEmpty(remarkString)) {
				remarkString = "您没有留言";
			}
			params.addBodyParameter("remarks", remarkString);
			params.addBodyParameter("goodsdetail", builder.toString());// 商品ID和商品数量，示例：1001_5,1102_6
			onRequest(SUBMMIT_ORDRE, UrlConstant.SUBMMIT_ORDERS,
					HttpMethod.POST, params);
		}
	}

	/**
	 * 判断信息是否为空
	 * 
	 * @return
	 */
	private boolean isNull() {
		// 判断是否有选择地址
		if (address == null) {
			MyToast.showHMToast(JfMallShopCartActivity.this, false, "请选择收货地址");
			return true;
		}
		// 判断积分是否够用
		int needScore = 0;
		needScore = Integer.valueOf(mTvNeedPay.getText().toString().trim());
		if (needScore > AppBasic.totalScore) {
			MyToast.showHMToast(JfMallShopCartActivity.this, false, "积分不足");
			return true;
		}
		// 判断购物车中是否有物品
		if (datas.size() < 1) {
			MyToast.showHMToast(JfMallShopCartActivity.this, false,
					"请先添加商品到购物车");
			return true;
		}

		return false;
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == CHOOSE_ADDRESS && arg2 != null && arg1 == RESULT_OK) {
			address = (Address) arg2.getSerializableExtra("address");
			mAddressLayout.setVisibility(View.VISIBLE);
			mTvAddress.setVisibility(View.GONE);
			isFromAddress = true;

			mTvAddressName.setText(address.addressee);
			mTvAddressDetail.setText(address.province + address.city
					+ address.area + address.address);
			mTvPhone.setText(address.cellPhoneNumber);
		}
	}

	/**
	 * 根据输入法的弹出，自动调整布局
	 * 
	 * @param rootView
	 *            界面根布局
	 * @param scrollView
	 *            需要滚动的布局
	 */
	private void controlKeybordLayout(final View rootView,
			final ListView scrollView) {

		globalLayoutListener = new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				Rect rect = new Rect();
				rootView.getWindowVisibleDisplayFrame(rect); // 获取当前view可视区域
				int unDisplayHeight = rootView.getRootView().getHeight()
						- rect.bottom; // 获取不可见区域的高度
				// 如果不可见区域的高度大于100，证明是输入法弹出来了
				// 先判断高度较之前是否有变化
				if (unDisplayHeight != oldUnDisplayHeight) {
					if (unDisplayHeight > 100) {
						// scrollView.smoothScrollToPosition(scrollView.getLastVisiblePosition());
						// scrollView.scrollTo(0,
						// unDisplayHeight/2-sureBlankOrgHeight);
						// scrollView.smoothScrollToPosition(scrollView.getLastVisiblePosition()+2);
						// sureBlankHideAnimal.start();
						sureBlankCon.setVisibility(View.VISIBLE);
					} else {
						LayoutParams params = bottomLayout.getLayoutParams();
						params.height = sureBlankOrgHeight;
						bottomLayout.setLayoutParams(params);
						sureBlankCon.setVisibility(View.GONE);
						Bitmap bitmap = BitmapFactory.decodeResource(
								getResources(), R.drawable.list_arrow_down);
						sureBlankCon.setImageBitmap(bitmap);
						sureBlankisUp = true;
					}
					oldUnDisplayHeight = unDisplayHeight;
				}
			}
		};

		rootView.getViewTreeObserver().addOnGlobalLayoutListener(
				globalLayoutListener);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AppBasic.getInstance().finishCurrentActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		
	}
	
	private void initVirtualconfirmDialog(final RequestParams params,final StringBuilder builder) {
		final String phone = PreferencesUtils.getString(mContext,
				PreferencesUtils.USER_PHONE);
		// 不可变部分，不再重新调用，只在第一次初始化生成
		if(myDialog == null) {
			myDialog = new MyDialog(mContext,R.layout.view_jfmall_virtual_confirm);
			RelativeLayout virtualInput = (RelativeLayout) myDialog
					.getContentView();
			// dialog事件
			virtual_close = (ImageView) virtualInput
					.findViewById(R.id.virtual_close);
			pay_way = (RadioGroup) virtualInput.findViewById(R.id.pay_way);
			native_sel_te = (TextView) virtualInput
					.findViewById(R.id.native_sel_te);
			remark1 = (EditText) virtualInput.findViewById(R.id.remark1);
			virtual_confirm = (TextView) virtualInput
					.findViewById(R.id.virtual_confirm);
			native_sel = (RadioButton) virtualInput
					.findViewById(R.id.native_sel);
			other_sel = (RadioButton) virtualInput.findViewById(R.id.other_sel);
			virtual_close.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					myDialog.dismiss();
					if(myDialog != null) {
						myDialog = null;
					}
				}
			});
			// 用户账户的手机号码
			native_sel_te.setText(phone);
		}
		// 默认选择本机号码,重置选项
		native_sel.setChecked(true);
		virtual_confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				remarkStr = remark1.getText().toString();
				if (pay_way.getCheckedRadioButtonId() == -1) {
					MyToast.showHMToast(JfMallShopCartActivity.this, false,
							"请先选择充值方式");
					return;
				}
				if (pay_way.getCheckedRadioButtonId() == other_sel.getId()) {
					if (TextUtils.isEmpty(remarkStr)) {
						MyToast.showHMToast(JfMallShopCartActivity.this,
								false, "请输入手机号码");
						return;
					}
					if (Utils.isEmojiCharacter(remarkStr)
							|| !Utils.MobilePattern.matcher(remarkStr)
									.matches()) {
						MyToast.showHMToast(JfMallShopCartActivity.this,
								false, "请输入正确的手机号码");
						return;
					}
					remarkString = remarkString + "充值号码：" + remarkStr;
					params.addBodyParameter("remarks", remarkString);
					params.addBodyParameter("goodsdetail",
							builder.toString());// 商品ID和商品数量，示例：1001_5,1102_6
					onRequest(SUBMMIT_ORDRE, UrlConstant.SUBMMIT_ORDERS,
							HttpMethod.POST, params);
				} else if (pay_way.getCheckedRadioButtonId() == native_sel
						.getId()) {
					if (TextUtils.isEmpty(phone)
							&& TextUtils.isEmpty(phone)) {
						MyToast.showHMToast(JfMallShopCartActivity.this,
								false, "请先为帐号绑定手机号码");
						return;
					}
					remarkString = remarkString + "充值号码：" + phone;
					params.addBodyParameter("remarks", remarkString);
					params.addBodyParameter("goodsdetail",
							builder.toString());// 商品ID和商品数量，示例：1001_5,1102_6
					onRequest(SUBMMIT_ORDRE, UrlConstant.SUBMMIT_ORDERS,
							HttpMethod.POST, params);
				}
				myDialog.dismiss();
				if(myDialog != null) {
					myDialog = null;
				}
				return;
			}
		});
	}

	@Override
	protected void onDestroy() {
		// 应用退出之后移除布局监听
		getWindow().getDecorView().findViewById(android.R.id.content)
				.getViewTreeObserver()
				.removeGlobalOnLayoutListener(globalLayoutListener);
		super.onDestroy();
	}
}
